#pragma once
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <string>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
using namespace std;

enum
{
    Sock_Err = 1,
    Bind_Err,
    Listen_Err,

};

class Socket
{
private:
    virtual void SockFd() = 0;
    virtual void Bind() = 0;
    virtual bool Connect(const string &ip, uint16_t port) = 0;
    virtual void Listen(int backlog) = 0;
    virtual Socket *Accept() = 0;

public:
    virtual int GetSockFd() = 0;
    virtual void SetSockFd(int sockfd) = 0;
    virtual void CloseFd(bool nolistenfd) = 0;

#define CV(addr) ((struct sockaddr *)(&(addr)))

    virtual ~Socket()
    {
    }

    void BuildTcpServer(int backlog)
    {
        SockFd();
        cout << "create listen success" << endl;
        Bind();
        cout << "bind success" << endl;
        Listen(backlog);
        cout << "listen success" << endl;
    }

    Socket *AcceptSockFd()
    {
        return Accept();
    }

    bool BiuldTcpClient(const string &ip, uint16_t port)
    {
        SockFd();
        return Connect(ip, port);
    }

    void BuildUdpServer()
    {
        SockFd();
        Bind();
    }

    void BiuldUdpClient()
    {
        SockFd();
    }
};

class TcpSocket : public Socket
{
public:
    TcpSocket()
    {
    }
    TcpSocket(uint16_t port) : _port(port), _listen(-1), _sockfd(-1)
    {
    }

    // void BuildTcpServer(int backlog)
    // {
    //     SockFd();
    //     cout << "create listen success" << endl;
    //     Bind();
    //     cout << "bind success" << endl;
    //     Listen(backlog);
    //     cout << "listen success" << endl;
    // }

    // Socket *AcceptSockFd(string *ip, uint16_t *port)
    // {
    //     return Accept(ip, port);
    // }

    // bool BiuldTcpClient(string &ip, uint16_t port)
    // {
    //     SockFd();
    //     return Connect(ip, port);
    // }

    int GetSockFd() override
    {
        return _sockfd;
    }

    void CloseFd(bool nolistenfd)
    {
        if (nolistenfd)
        {
            close(_sockfd);
            cout << "close sockfd: " << _sockfd << endl;
        }
        else
        {
            cout << "close _listenfd: " << _listen << endl;
            close(_listen);
        }
    }

    ~TcpSocket()
    {
    }

private:
    void SockFd() override
    {
        _listen = socket(AF_INET, SOCK_STREAM, 0);
        if (_listen < 0)
        {
            exit(Sock_Err);
        }
        cout << "open" << _listen << endl;

        int opt = 1;
        setsockopt(_listen, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt));
    }

    void Bind() override
    {
        struct sockaddr_in local;
        memset(&local, 0, sizeof(local));
        local.sin_port = htons(_port);
        local.sin_family = AF_INET;
        local.sin_addr.s_addr = INADDR_ANY;
        int n = bind(_listen, CV(local), sizeof(local));
        if (n < 0)
        {
            exit(Bind_Err);
        }
    }

    void Listen(int backlog) override
    {
        int n = listen(_listen, backlog);
        if (n < 0)
        {
            exit(Listen_Err);
        }
    }

    Socket *Accept() override
    {
        struct sockaddr_in peer;
        socklen_t len = sizeof(peer);
        int sockfd = accept(_listen, CV(peer), &len);

        if (sockfd >= 0)
        {
            char buffer[128];
            inet_ntop(AF_INET, &peer.sin_addr, buffer, sizeof(buffer));
            Socket *newsock = new TcpSocket();
            // cout << "new sock: " << newsock << endl;
            newsock->SetSockFd(sockfd);
            cout << "create new sockfd success, new sockfd: " << sockfd << endl;

            return newsock;
        }
        else
            return nullptr;
    }

    bool Connect(const string &ip, uint16_t port) override
    {
        struct sockaddr_in server;
        memset(&server, 0, sizeof(server));
        server.sin_port = htons(port);
        server.sin_family = AF_INET;
        // server.sin_addr.s_addr = INADDR_ANY;
        inet_pton(AF_INET, ip.c_str(), &server.sin_addr);
        _sockfd = _listen;

        int n = connect(_listen, CV(server), sizeof(server));

        if (n >= 0)
        {
            cout << "connect success" << endl;
            return true;
        }
        else
            return false;
    }

    void SetSockFd(int sockfd) override
    {
        _sockfd = sockfd;
    }

    int _listen;
    int _sockfd;
    int _port;
};